
package com.li.controller;

import com.li.mapper.AllsortsMapper;
import com.li.pojo.Allsorts;
import com.li.pojo.Course1;
import com.li.pojo.Course2;
import com.li.service.AllsortsService;
import com.li.service.Course1Service;
import com.li.service.Course2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.List;

@Controller
public class AllsortsController {
    @Resource
    AllsortsService service;
    @Resource
    Course1Service service1;
    @Resource
    Course2Service service2;
    int sum1=0,sum2=0,sum3=0,sum4=0,time=0,sum=0;
    int s1,s2,s3,s4,s5,s6;
    @RequestMapping("/allsort")
    public String list(Model model){
        List<Course1> list1=service1.allCourse1();
        List<Course2> list2=service2.allCourse2();

        Allsorts allsorts = new Allsorts();
        for(Course1 c1:list1){
            if(c1.getCsort().equals("通识教育必修课")){
                time=time+Integer.parseInt(c1.getCtotal());
                if(Integer.parseInt(c1.getCtheory())>0)
                    sum1=sum1+c1.getCredit();
                else
                    sum3=sum3+c1.getCredit();
            }
        }
         s1=sum1+sum2+sum3+sum4;
        allsorts.setAsort("通识教育必修课");
        allsorts.setAcredit(s1);
        allsorts.setAperiod(time);
        allsorts.setAtheory(sum1);
        allsorts.setApractice(sum2);
        allsorts.setAexp(sum3);
        allsorts.setAclass2(sum4);
        //allsorts.setAcredit_bi(s1/sum);
        service.updateAllsorts(allsorts);
        sum1=0;sum2=0;sum3=0;sum4=0;time=0;
        for(Course1 c1:list1){
            if(c1.getCsort().equals("通识教育选修课")){
                time=time+Integer.parseInt(c1.getCtotal());
                if(Integer.parseInt(c1.getCtheory())>0)
                    sum1=sum1+c1.getCredit();
                else
                    sum3=sum3+c1.getCredit();
            }
        }
         s2=sum1+sum2+sum3+sum4;
        allsorts.setAsort("通识教育选修课");
        allsorts.setAcredit(sum1+sum2+sum3+sum4);
        allsorts.setAperiod(time);
        allsorts.setAtheory(sum1);
        allsorts.setApractice(sum2);
        allsorts.setAexp(sum3);
        allsorts.setAclass2(sum4);
        // allsorts.setAcredit_bi(s2/sum);
        service.updateAllsorts(allsorts);
        sum1=0;sum2=0;sum3=0;sum4=0;time=0;
        for(Course1 c1:list1){
            if(c1.getCsort().equals("学科必修课")){
                time=time+Integer.parseInt(c1.getCtotal());
                if(Integer.parseInt(c1.getCtheory())>0)
                    sum1=sum1+c1.getCredit();
                else
                    sum3=sum3+c1.getCredit();
            }
        }
         s3=sum1+sum2+sum3+sum4;
        allsorts.setAsort("学科必修课");
        allsorts.setAcredit(sum1+sum2+sum3+sum4);
        allsorts.setAperiod(time);
        allsorts.setAtheory(sum1);
        allsorts.setApractice(sum2);
        allsorts.setAexp(sum3);
        allsorts.setAclass2(sum4);
        // allsorts.setAcredit_bi(s2/sum);
        service.updateAllsorts(allsorts);
        sum1=0;sum2=0;sum3=0;sum4=0;time=0;
        for(Course1 c1:list1){
            if(c1.getCsort().equals("专业必修课")){
                time=time+Integer.parseInt(c1.getCtotal());
                if(Integer.parseInt(c1.getCtheory())>0)
                    sum1=sum1+c1.getCredit();
                else
                    sum3=sum3+c1.getCredit();
            }
        }
        s4=sum1+sum2+sum3+sum4;
        allsorts.setAsort("专业必修课");
        allsorts.setAcredit(sum1+sum2+sum3+sum4);
        allsorts.setAperiod(time);
        allsorts.setAtheory(sum1);
        allsorts.setApractice(sum2);
        allsorts.setAexp(sum3);
        allsorts.setAclass2(sum4);
        //allsorts.setAcredit_bi(s3/sum);
        service.updateAllsorts(allsorts);
        sum1=0;sum2=0;sum3=0;sum4=0;time=0;
        for(Course1 c1:list1){
            if(c1.getCsort().equals("专业选修课")){
                time=time+Integer.parseInt(c1.getCtotal());
                if(Integer.parseInt(c1.getCtheory())>0)
                    sum1=sum1+c1.getCredit();
                else
                    sum3=sum3+c1.getCredit();
            }
        }
        s5=sum1+sum2+sum3+sum4;
        allsorts.setAsort("专业选修课");
        allsorts.setAcredit(sum1+sum2+sum3+sum4);
        allsorts.setAperiod(time);
        allsorts.setAtheory(sum1);
        allsorts.setApractice(sum2);
        allsorts.setAexp(sum3);
        allsorts.setAclass2(sum4);
        // allsorts.setAcredit_bi(s4/sum);
        service.updateAllsorts(allsorts);
        sum1=0;sum2=0;sum3=0;sum4=0;time=0;
        for(Course2 c2:list2){
            if(c2.getCsort().equals("实践环节")){
                sum2=sum2+c2.getCredit();

            }
        }
         s6=sum1+sum2+sum3+sum4;
        allsorts.setAsort("实践环节");
        allsorts.setAcredit(sum1+sum2+sum3+sum4);
        allsorts.setAperiod(time);
        allsorts.setAtheory(sum1);
        allsorts.setApractice(sum2);
        allsorts.setAexp(sum3);
        allsorts.setAclass2(sum4);
        //allsorts.setAcredit_bi(s5/sum);
        service.updateAllsorts(allsorts);
        sum1=0;sum2=0;sum3=0;sum4=0;time=0;
        sum=s1+s2+s3+s4+s5+s6;
        Collection<Allsorts> allsortss=service.list();
        model.addAttribute("allsortss",allsortss);
        return "allsort/list";
    }
    @RequestMapping("/updateallsort")
    public String update(){
        NumberFormat nt = NumberFormat.getPercentInstance();//获取格式化对象
        nt.setMinimumFractionDigits(2);//设置百分数精确度2即保留两位小数
         System.out.println(s1+" "+sum+" "+s1/sum+" "+nt.format(s1/sum));
        Allsorts allsorts = new Allsorts();
        allsorts.setAsort("通识教育必修课");
        allsorts.setAcredit_bi(nt.format((double)s1/sum));
        service.updatebi(allsorts);
        allsorts.setAsort("通识教育选修课");
        allsorts.setAcredit_bi(nt.format((double)s2/sum));
        service.updatebi(allsorts);
        allsorts.setAsort("学科必修课");
        allsorts.setAcredit_bi(nt.format((double)s3/sum));
        service.updatebi(allsorts);
        allsorts.setAsort("专业必修课");
        allsorts.setAcredit_bi(nt.format((double)s4/sum));
        service.updatebi(allsorts);
        allsorts.setAsort("专业选修课");
        allsorts.setAcredit_bi(nt.format((double)s5/sum));
        service.updatebi(allsorts);
        allsorts.setAsort("实践环节");
        allsorts.setAcredit_bi(nt.format((double)s6/sum));
        service.updatebi(allsorts);
        return "redirect:/allsort";
    }
}

